package com.grape.dsaa.lineartable.list.core;

import java.util.*;

/**
 * 描述：自定义实现arrayList
 * @author: myx
 * @date: 2019-04-28
 * 注意：本内容仅限于学习使用
 * Copyright © 2019-myx. All rights reserved.
 */
public class MyArraylist<E> implements List<E> {
    /**
     * 默认初始值
     */
    private static final int DEFAULT_CAPACITY = 10;

    /**
     * 数据
     */
    private E[] data;
    /**
     * 当前数据长度
     */
    private int size;

    public MyArraylist(int capacity) {
        this.size = 0;
        data = (E[]) new Object[capacity];
    }

    public MyArraylist() {
        this(DEFAULT_CAPACITY);
    }

    @Override
    public int size() {
        return size;
    }

    @Override
    public boolean isEmpty() {
        return size==0;
    }

    @Override
    public boolean contains(Object o) {
        for(int i=0; i<size; i++) {
            if(data[i].equals(o)){
                return true;
            }
        }
        return false;
    }

    @Override
    public Iterator<E> iterator() {
        //不实现
        return null;
    }

    @Override
    public Object[] toArray() {
        //不实现
        return new Object[0];
    }

    @Override
    public <T> T[] toArray(T[] a) {
        //不实现
        return null;
    }

    @Override
    public boolean add(E e) {
        return false;
    }

    @Override
    public boolean remove(Object o) {
        return false;
    }

    @Override
    public boolean containsAll(Collection<?> c) {
        return false;
    }

    @Override
    public boolean addAll(Collection<? extends E> c) {
        return false;
    }

    @Override
    public boolean addAll(int index, Collection<? extends E> c) {
        return false;
    }

    @Override
    public boolean removeAll(Collection<?> c) {
        return false;
    }

    @Override
    public boolean retainAll(Collection<?> c) {
        return false;
    }

    @Override
    public void clear() {

    }

    @Override
    public E get(int index) {
        if(index < 0 || index >= size){
            throw new IllegalArgumentException("数组小标越界...");
        }
        return data[index];
    }

    @Override
    public E set(int index, E element) {
        if(index < 0 || index >= size){
            throw new IllegalArgumentException("数组小标越界...");
        }
        data[index] = element;
        return null;
    }

    @Override
    public void add(int index, E element) {
        if(index < 0 || index > size) {
            throw new IllegalArgumentException("数组小标越界...");
        }
        if(size == data.length) {
            grow(2*data.length);
        }

        for(int i=size-1; i>=index; i--) {
            data[i+1] = data[i];
        }
        data[index] = element;
        size++;
    }

    @Override
    public E remove(int index) {
        if(index < 0 || index >= size) {
            throw new IllegalArgumentException("数组小标越界...");
        }
        E val = data[index];
        for(int i = index + 1; i<size; i++) {
            data[i-1] = data[i];
        }
        size--;
        data[size] = null;
        if(size < (data.length >> 1)) {
            grow(data.length/2);
        }
        return val;
    }

    @Override
    public int indexOf(Object o) {
        for(int i=0; i<size; i++) {
            if(data[i].equals(o)){
                return i;
            }
        }
        return -1;
    }

    @Override
    public int lastIndexOf(Object o) {
        return 0;
    }

    @Override
    public ListIterator<E> listIterator() {
        return null;
    }

    @Override
    public ListIterator<E> listIterator(int index) {
        return null;
    }

    @Override
    public List<E> subList(int fromIndex, int toIndex) {
        return null;
    }

    private void grow(int newCapacity) {
	/*	E[] newData = (E[]) new Object[newCapacity];
		for(int i=0; i<data.length; i++)
			newData[i] = data[i];

		data = newData;*/
        data = Arrays.copyOf(data, newCapacity);
    }
}
